function check(tbl){
	try {
		var appid=tbl.appid||req.getHeader("appid");
		var secret=tbl.secret||req.getHeader("secret");
		if(isEmpty(appid)){
			var data=new Record();
			tbl.forEach(function(k,v){
				if(k.startsWith("{")&&k.endsWith("}")) {
					data.putAll(foJson(k))
				}
			})
			appid=data.appid;
			secret=data.secret;
		}
		var num=$count("open",Cnd.where("appid","=",appid).and("secret","=",secret))
		if(num>0){
			return true;
		}
	} catch (e) {
		
	}
	return false;
}
function cates(tbl){
	if(!check(tbl)){
		return {"code":10006,"msg":"appid or secret error!"}
	}
	var cnd=Cnd.where("1","=",1)
	if(!isEmpty(tbl.pid)){
		cnd.and("pid","=",tbl.pid)
	}else{
		cnd.and("pid","=",0)
	}
	var list=$list("cate",cnd)
	return list;
}
function courses(tbl){
	if(!check(tbl)){
		return {"code":10006,"msg":"appid or secret error!"}
	}
	var cnd=Cnd.where("price","=",0).and("id",">",460).and("id","<",599)
	if(!isEmpty(tbl.cid1)){
		cnd.and("cid1","=",tbl.cid1);
	}
	if(!isEmpty(tbl.cid2)){
		cnd.and("cid2","=",tbl.cid2)
	}
	var list=$list("course",cnd)
	list.forEach(function(c){
		c.remove("name")
		c.put("img","http://www.iepsy.com/"+c.img)
		c.remove("update_time")
		c.remove("duration")
		c.remove("section")
		c.remove("bnum")
		c.remove("fnum")
		c.remove("is_tj")
		c.remove("html")
		c.remove("leve")
		c.remove("vid")
		c.remove("vip")
		c.remove("agency_id")
		c.remove("is_zb")
		c.remove("is_file")
		c.remove("is_exam")
		c.remove("type")
		c.remove("pct")
	})
	return list;
}
function chapter(tbl){
	if(!check(tbl)){
		return {"code":10006,"msg":"appid or secret error!"}
	}
	var cnd=Cnd.where("tbl_name","=","course").and("tbl_id","=",tbl.id).and("pid","=",0)
	var list=$list("chapter",cnd)
	list.forEach(function(c){
		c.put("list",$list("chapter",Cnd.where("pid","=",c.id)))
	})
	return list;
}
function video(tbl){
	if(!check(tbl)){
		return {"code":10006,"msg":"appid or secret error!"}
	}
	var video=dao.fetch("video",Cnd.where("id","=",tbl.id))
	var api=Java.type("com.jse.plug.AliyunApi");
	var map=new NutMap();
	map.put("VideoId",video.VideoId)
	var body=api.exec("GetVideoPlayAuth",map)
	var rtv=foJson(body)
	map.put("CoverURL",video.CoverURL)
	map.put("PlayAuth",rtv.PlayAuth)
	map.put("width",tbl.width||"100%")
	map.put("height",tbl.height||"100%")
	return map;
}
function mp4(tbl){
	if(!check(tbl)){
		return {"code":10006,"msg":"appid or secret error!"}
	}
	var video=dao.fetch("video",Cnd.where("id","=",tbl.id))
	var api=Java.type("com.jse.plug.AliyunApi");
	var map=new NutMap();
	map.put("VideoId",video.VideoId)
	map.put("Formats","mp4")
	var body=api.exec("GetPlayInfo",map)
	var rtv=foJson(body)
	var list=rtv.PlayInfoList.PlayInfo;
	//var rs=list.stream().filter(x->x.getString("Definition").equals("FD")).findFirst().get();
	var rs=list.stream().min(new java.util.Comparator() {
		compare:function(o1,o2) {
			return o1.get("Size").compareTo(o2.get("Size"));
		}
	}).get();
	return rs;
}
function teacher(tbl){
	if(!check(tbl)){
		return {"code":10006,"msg":"appid or secret error!"}
	}
	var cnd=Cnd.where("name","=",tbl.name)
	var t=$fetch("teacher",cnd)
	t.put("img","http://www.iepsy.com/"+t.img)
	return t;
}